package org.ricks.log;

import java.io.File;

/**
 * @author chenwei
 * @Title:
 * @Package
 * @Description:
 * @date 2021/1/2917:14
 */
public final class FileTuple {

    private final File original;
    private final File backup;

    /**
     * @param original
     *            Original file
     * @param backup
     *            Backup file
     */
    public FileTuple(final File original, final File backup) {
        this.original = original;
        this.backup = backup;
    }

    /**
     * Gets the original file.
     *
     * @return Original file
     */
    public File getOriginal() {
        return original;
    }

    /**
     * Gets the backup file.
     *
     * @return Backup file
     */
    public File getBackup() {
        return backup;
    }

    /**
     * Gets the last modification time.
     *
     * @return Last modification time
     */
    public long getLastModified() {
        return Math.max(original.lastModified(), backup.lastModified());
    }

    /**
     * Deletes the original and backup file.
     */
    public void delete() {
        if (original.isFile() && !original.delete()) {
            InternalLogger.log(Level.WARN, "Failed to delete log file '" + original + "'");
        }
        if (!backup.equals(original) && backup.isFile() && !backup.delete()) {
            InternalLogger.log(Level.WARN, "Failed to delete backup file '" + backup + "'");
        }
    }

}
